﻿@namespace Oqtane.Modules.Admin.Pages
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IPageService PageService

@if (PageState.Pages != null)
{
    <ActionLink Action="Add" Text="Add Page" />

    <Pager Items="@PageState.Pages.Where(item => !item.IsDeleted)">
        <Header>
            <th style="width: 1px;">&nbsp;</th>
            <th style="width: 1px;">&nbsp;</th>
            <th>Name</th>
        </Header>
        <Row>
            <td><ActionLink Action="Edit" Parameters="@($"id=" + context.PageId.ToString())" /></td>
            <td><ActionDialog Header="Delete Page" Message="@("Are You Sure You Wish To Delete The " + context.Name + " Page?")" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeletePage(context))" /></td>
            <td>@(new string('-', context.Level * 2))@(context.Name)</td>
        </Row>
    </Pager>
}

@code {
    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Admin;

    private async Task DeletePage(Page page)
    {
        try
        {
            page.IsDeleted = true;
            
            await PageService.UpdatePageAsync(page);
            await logger.LogInformation("Page Deleted {Page}", page);
            NavigationManager.NavigateTo(NavigateUrl("admin/pages"));
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Deleting Page {Page} {Error}", page, ex.Message);
            AddModuleMessage("Error Deleting Page", MessageType.Error);
        }
    }
}
